import { Component, Input, OnInit, ViewChild } from '@angular/core';
import { ColumnType, SPage, TablePageComponent } from '@shared/components/table-page/table-page.component';
import { Body, _HttpClient } from '@delon/theme';
import { NzMessageService, NzModalService,NzModalRef } from 'ng-zorro-antd';
import { ApiService } from '@core/services/api';
import { STData } from '@delon/abc';

@Component({
  selector: 'change-edit',
  templateUrl: './change-edit.component.html',
  styles: ['>>>.w200{width:200px;}']
})
export class ChangeEditComponent implements OnInit {
  @ViewChild('st', { static: false }) st: TablePageComponent;
  selectData: any[];
  loading = false;
  addUserInfo = {
    userAccount: '',
    userPassword: '',
    userName: '',
    roles: [],
    depts: []
  };
  @Input()
  set _param(value) {
    if (!value) return;
    this.param = value;
  };
  param: any;
  config: SPage = {
    columns: [
      {
        index: 'deviceCode',
        title: '设备SN',
        className:'w200',
        filter: {
          placeholder: '请输入设备SN',
          type: 'string',
        }
      },
      {
        index:'deviceCategory',
        title:'设备分类',
        type: ColumnType.tag,
        tag: {
          1:{text: '门禁'},
          2:{text: 'IPC'},
          3:{text: 'NVR'}
        },
      },
      {
        index:'deviceIp',
        title:'设备IP地址',
        filter: {
          placeholder: '请输入设备IP',
          type: 'string',
        }
      },
      {
        index: 'deviceType',
        title: '设备类型',
        type: ColumnType.tag,
        tag:{
          1:{text: '门禁摄像机'},
          2:{text: '门禁刷卡'},
          3:{text: 'IPC'},
          4:{text: 'NVR'},
          5:{text: '微型门禁产品'},
          6:{text: '安卓门禁产品'}
        }
      },
      {
        index: 'deviceLocationStatus',
        title: '绑定状态',
        type: ColumnType.tag,
        tag: {
          0:{text: '未绑定',color: 'red'},
          1:{text: '已绑定',color: 'green'}
        }
      }],
    showCheckBox: false,
    reqUrl:'/core/host/page',
    defaultParam:{deviceLocationStatus:'0'},
    editBtn:[
      { title: '绑定' }
    ]
  }
  constructor(private http: _HttpClient,  private api: ApiService, private modal: NzModalService,private ref: NzModalRef, private message: NzMessageService) { }
  
  ngOnInit() {
    
  }
  operate(e: any): void {
    if (e.title === '绑定') {
      this.exchange(e.data);
    }
  }
  exchange(e){

    this.http.post('/core/host/switchover', {deviceId:this.param.id,newDeviceId:e.id}).subscribe((res:any) => {
      if (res.status == '200') {
        this.message.success('换机成功！');
        this.ref.triggerOk();
      }else{
        this.message.error(res.message);
      }
    })
    
  }
  _console(e: any): void {
    this.selectData = e;
  }
  

}
